#ifndef CACHE
#define CACHE

#include "typedef.hpp"

enum MESI
{
	M = 1,
	E = 2,
	S = 4,
	I = 8
};

using MESIMeta = std::pair<MESI, uint32_t>;
using CacheLine = std::tuple<std::array<char, 64>, MESI, uint32_t>;
using Cache = std::pair<CacheLine[4], int>;

bool write(Cache &, uint32_t, uint8_t, uint8_t, uint64_t *, Semaphore<MESIMeta> *);
void update(Cache &, CacheLine &, uint32_t, uint16_t);
CacheLine *search(Cache &, uint32_t);

class MemCore
{
private:
	MessageBus *bus;
	std::array<CacheLine, 1024> *l1i;
	std::array<Cache, 512> *l1d;
	std::array<Cache, 1024> *l2;
	Semaphore<MESIMeta> *message;

public:
	MemCore() = default;
	MemCore(MessageBus *, Semaphore<MESIMeta> *);
	~MemCore() = default;
	// 操作函数
	void load();
	void store();
	void change();
	void shutdown();
	// 程序主循环
	void run();
};

#endif
